package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqDeodorizationRunningFourMapper;
import com.guofen.pojo.SjqDeodorizationRunningFour;
import com.guofen.service.sjq.SjqDeodorizationRunningFourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqDeodorizationRunningFourServiceImpl implements SjqDeodorizationRunningFourService {

    @Autowired
    private SjqDeodorizationRunningFourMapper sjqDeodorizationRunningFourMapper;


    @Override
    public boolean addForm(String date, String killRange, String killDrugName, String proportioningDegree, Double consumption) {
        SjqDeodorizationRunningFour sjqDeodorizationRunningFour = new SjqDeodorizationRunningFour();
        sjqDeodorizationRunningFour.setDate(date);
        sjqDeodorizationRunningFour.setKillRange(killRange);
        sjqDeodorizationRunningFour.setKillDrugName(killDrugName);
        sjqDeodorizationRunningFour.setProportioningDegree(proportioningDegree);
        sjqDeodorizationRunningFour.setConsumption(consumption);
        int result = sjqDeodorizationRunningFourMapper.insert(sjqDeodorizationRunningFour);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqDeodorizationRunningFour> list = sjqDeodorizationRunningFourMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqDeodorizationRunningFour> list = sjqDeodorizationRunningFourMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqDeodorizationRunningFourMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqDeodorizationRunningFour record) {
        return sjqDeodorizationRunningFourMapper.updateByPrimaryKeySelective(record);
    }
}
